<template>
  <div class="ChildPage TablePage" v-loading="loading">
    <vb-page ref="page">
      <template #table-handle-prepend="scope">
        <el-button size="mini" type="text" icon="el-icon-edit" @click="handleSync(scope.row)">手动同步</el-button>
      </template>
      <template #headerSearch-btn-append="{ searchForm }">
        <el-button size="small" type="text" icon="el-icon-share" style="color: #66CCFF" @click="syncTimeSet">同步配置</el-button></template>
      <template #table-stockNo="scope">
        {{ getStockName(scope.row.stockNo) }}
      </template>
      <template #table-count="scope">
        {{ scope.row.count? Number(scope.row.count.toFixed(6)): scope.row.count }}
      </template>
      <template #table-isUpload="scope">
        <el-tag :type="scope.row.isUpload === 'yes'? 'success': 'info'" >{{ scope.row.isUpload === 'yes'? '已上传': '未上传' }}</el-tag>
      </template>
    </vb-page>
  </div>
</template>

<script>
import syncTime from "../jikeyun/syncTime.vue";
export default {
  data() {
    return {
      materialLoading: false,
      reloadMaterial: false,
      materialList: [],
      dataList: []
    }
  },
  components: {
    syncTime
  },
  async mounted() {
    await this.getStock()
    await this.selectStock()
  },
  methods: {
    async syncTimeSet(){
      window.open(import.meta.env.VITE_EXPORT_URL + `syncTime`)
    },
    async handleSync(row) {
      try {
        if (row.orderType === '调拨单') {
           this.syncListAllocationLatest(row.billTaskNo);
        } else if (row.orderType === '销售单') {
           this.syncListSaleLatest(row.billTaskNo);
        } else {
          this.$message.warning('单据类型未知');
        }
      } catch (error) {
        this.$message.error(`同步失败: ${error.message}`);
      }
    },
    //调拨单同步金蝶
    async syncListAllocationLatest(allocationId) {
      try {
        await this.$confirm('是否同步此调拨单到金蝶', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'success'
        });
        this.$message({ type: 'success', message: '正在同步' });
        this.loading = true;
        let res = await this.callApi('sync_list_allocation_latest', {allocationId: allocationId});
        if (!res.res) {
          this.$message.warning('同步未成功，请稍后重试或联系管理员');
          return;
        }
        //if (!res.res) return;
        this.$message.success('同步成功');
        // 刷新表格数据
        this.reload();
      } catch (error) {
        if (error === 'cancel') {
          this.$message.warning('取消同步');
        } else {
          this.$message.error('同步失败');
          console.error(error);
        }
      } finally {
        this.loading = false;
      }
    },
    //销售单同步金蝶
    async syncListSaleLatest(tradeNo) {
      try {
        await this.$confirm('是否同步此销售单到金蝶', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'success'
        });
        this.$message({ type: 'success', message: '正在同步' });
        this.loading = true;
        let res = await this.callApi('sync_list_sale_latest', { tradeNo: tradeNo});
        if (!res.res) return;
        this.$message.success('同步成功');
      } catch (error) {
        if (error === 'cancel') {
          this.$message.warning('取消同步');
        } else {
          this.$message.error('同步失败');
        }
      } finally {
        this.loading = false;
      }
    },
    async doDownload(searchForm) {
      this.loading = true
      try {
        let res = await this.callApi('download_pur_in_list', searchForm)
        if (!res.res) return
      } finally {
        this.loading = false
      }
    },
    async selectStock() {
      this.loading = true
      try {
        let res = await this.callApi('get_stock')
        if (!res.res) return
        this.dataList = res.data
      } finally {
        this.loading = false
      }
    },
    async getMaterialList(val) {
      if (!val) {
        this.materialList = []
        return
      }
      this.materialLoading = true
      try {
        const res = await this.callApi('get_sync_list_latest', { key: val, pageSize: 20 })
        if (!res.res) return
        this.materialList = res.data
      } finally {
        this.materialLoading = false
      }
    },
    toExport(billNo,stockNo) {
      window.open(import.meta.env.VITE_EXPORT_URL + `purInExport?billNo=${ billNo}&stockNo=${stockNo}`)
    },
    async getStock() {
      this.loading = true;
      try {
        let res = await this.$api.getChildDictionaryListByKeys({
          keys: ['stock']
        });
        if (!res.res) return;
        this.stockList = res.stock;
      } finally {
        this.loading = false;
      }
    },
    /**
     * 获取状态名
     */
    getStockName(val) {
      if (this.stockList && this.stockList.length > 0) {
        for(let i = 0; i < this.stockList.length; i++) {
          if (this.stockList[i].key === val) {
            return this.stockList[i].name;
          }
        }
      }
      return '';
    }
  }
}
</script>

<style scoped>

</style>
